<template>
	<view class="my-settle-container">
		<!-- 全选 -->
		<view class="settle-check-box">
			<radio :checked="isFullCheck" color="#c00000" @click="allCheckHandler" />
			<text>全选</text>
		</view>
		<!-- 合计 -->
		<view class="sum-box">
			<text>合计: </text>
			<text class="price-box">${{ checkedAllSum }}</text>
		</view>
		<!-- 结算 -->
		<view class="settle-sum-box" @click="settleMent">
			<text>结算({{checkCount}})</text>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-settle",
		computed: {
			// 选中的数量
			...mapGetters('m_cart', ['checkCount', 'total', 'checkedAllSum']),
			...mapGetters('m_user', ['addStr']),
			...mapState('m_user', ['token']),
			...mapState('m_cart', ['cart']),
			// 是否全选
			isFullCheck() {
				return this.checkCount === this.total
			}
		},
		data() {
			return {
				// 定时器
				seconds: 3,
				// 关闭定时器
				timer: null
			};
		},
		methods: {
			...mapMutations('m_cart', ['updateAllGoodsState']),
			...mapMutations('m_user', ['updateRedirectInfo']),
			/* 
			 全选的状态
			 */
			allCheckHandler(e) {
				// this.updateAllGoodsState(!(this.checkCount === this.total))
				this.updateAllGoodsState(!this.isFullCheck)
			},
			/* 
			 结算商品
			 */
			settleMent() {
				// 判断是否有选中的商品
				if (!this.checkCount) return uni.$showMsg('请选择要结算的商品!')
				// 判断是否填写了收货地址
				if (!this.addStr) return uni.$showMsg('请选择收货地址!')
				// 判断是否登录了
				// if (!this.token) return uni.$showMsg('请先登录!')
				if (!this.token) return this.delayNavigate()
				this.payOrder()
			},
			/* 
			 倒计时的提示消息
			 */
			showTips(n) {
				uni.showToast({
					title: '请登录后结算 ' + n + ' 秒后跳转到登录页',
					icon: 'none',
					duration: 1500,
					mask: true
				})
			},
			/* 
			 定时器的开关
			 */
			delayNavigate() {
				// if (this.seconds < 0) return clearInterval(this.timer)
				this.seconds = 3
				this.showTips(this.seconds)
				this.timer = setInterval(() => {
					this.seconds--
					if (this.seconds <= 0) {
						clearInterval(this.timer)
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.updateRedirectInfo({
									openType: 'switchTab',
									form: '/pages/cart/cart'
								})
							}
						})
						return
					}
					this.showTips(this.seconds)
				}, 1000)
			},
			/* 
			创建订单 
			 */
			async payOrder() {
				const filterArr = this.cart.filter(item => item.goods_state)
				const mapArr = filterArr.map(item => {
					return {
						goods_id: item.goods_id,
						goods_number: item.goods_count,
						goods_price: item.goods_price
					}
				})
				// 创建订单接口所需要的对象
				const orderInfo = {
					// 商品的价格
					order_price: 0.01,
					// 商品的收货地址
					consignee_addr: this.addStr,
					// 商品的信息
					goods: mapArr
				}
				const {
					data: res
				} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				if (res.meta.status !== 200) return uni.$showMsg('创建订单失败!')
				const orderNumber = res.message.order_number || ''
				// 订单预支付
				this.unifiedorder(orderNumber)
			},
			/* 
			 订单预支付
			 */
			async unifiedorder(orderNumber) {
				const {
					data: res
				} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
					orderNumber
				})
				if (res.meta.status !== 200) return uni.$showMsg('预付订单生成失败!')
				const payInfo = res.message.pay
				// 发起微信支付
				console.log(res, '订单与之符');
				this.chkOrder(payInfo, orderNumber)
			},
			/* 
			 发起微信支付
			 */
			async chkOrder(payInfo, orderNumber) {
				// uni.requestPayment({
				// 	// 时间戳
				// 	timeStamp: '',
				// 	// 随机字符
				// 	nonceStr: '',
				// 	// 统一下单的id
				// 	package: '',
				// 	// 微信签名
				// 	paySign: ''
				// })
				const [err, succ] = await uni.requestPayment(payInfo)
				if (!err) return uni.$showMsg('订单未支付!')
				// 完成订单支付之后,查询订单的状态,通过订单编号进行查询
				const {
					data: res
				} = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {
					orderNumber
				})
				if (res.meta.status !== 200) return uni.$showMsg('订单未支付!')
				// 查询订单支付成功之后,进行提示
				uni.showToast({
					title: '支付完成',
					icon: 'success'
				})
			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container {
		height: 50px;
		background-color: #ffffff;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 999;
		padding-left: 6px;
		padding-right: 3px;
		box-sizing: border-box;
		font-size: 14px;

		.settle-check-box {
			display: flex;
			align-items: center;
		}

		.sum-box {
			.price-box {
				color: #c00000;
				font-weight: bold;
			}
		}

		.settle-sum-box {
			min-width: 100px;
			height: 100%;
			color: #ffffff;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #c00000;
			padding: 0 10px;
		}
	}
</style>